import fs from 'fs'
// const nodeFetch = require('node-fetch')
import { createApi } from 'unsplash-js'

// global.fetch = fetch;


const ACCESS_KEY = 'ov4aCRJtM89EF-_XLVJFLS4cLkb1kC0j2fjtXyUkzl4'
// const SECRET_KEY = 'sPxcHJ7QuJScS9Q1dOJ4k2L4RR6FDpBzO74_qw4IIa0'

const unsplash = createApi({
  accessKey: ACCESS_KEY,
  // secret: SECRET_KEY,
  // fetch: nodeFetch
})


async function loadImage(word) {
  const result =await unsplash.search.getPhotos({
    query: word,
    perPage: 3
  })
  if (result.errors) {
    console.log(response.errors[0])
    return
  }

  const response = result.response

  response.results.forEach(item => {
    item.urls.thumb
  })

  // const info = await response.json()
  fs.writeFileSync(`public/images/${word}.json`, JSON.stringify(response, null, 2), { encoding: 'utf-8' })

  // const binData = await response.blob()
  // fs.writeFileSync(`public/images/${word}.jpg`, binData)
}

loadImage('cat')
